<template>
  <div class="file-explorer-button">
    <ElButton
      size="mini"
      :disabled="!selectedRows.length || containFolder"
      icon="el-icon-download"
      @click="downloadFile()">
      下载
    </ElButton>
  </div>
</template>

<script>
  import { downloadFiles } from '@/api/file'

  export default {
    name: 'DownloadFileButton',
    inject: ['explorer'],
    props: {
      selectedRows: {
        type: Array,
        default: () => []
      }
    },
    computed: {
      containFolder() {
        return (
          this.selectedRows &&
          this.selectedRows.length > 0 &&
          this.selectedRows.some(value => value.type === 'directory')
        )
      }
    },
    methods: {
      async downloadFile(file) {
        if (!file && !this.selectedRows.length) {
          this.$message.warning('请选择要下载的文件！')
          return
        }
        await downloadFiles(file ? [file.id] : this.selectedRows.map(value => value.id))
      }
    }
  }
</script>
